<?php

namespace app\api\controller;

use app\api\model\RecitingMission;
use app\api\model\RecitingQuestionAnswer;
use app\api\model\Homework;
use app\admin\model\User;
use app\common\controller\Api;
use fast\Random;
use think\Request;

class NewWrong extends Api
{


    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    /**
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 错题列表
     * dxt
     */
    public function index()
    {
        $page = input('page/d')&&is_numeric(input('page/d')) ? input('page/d') : 1;
        $pageSize = input('pageSize/d')&&is_numeric(input('pageSize/d')) ? input('pageSize/d') : 10;
        $userid = $this->auth->id;
        $list = RecitingMission::where(['user_id'=>$userid,'mission_type'=>0])->limit(($page - 1)*$pageSize,$pageSize)->select();
        $ids = array_column($list, 'mission_id');
        $datas = Homework::whereIn('id',$ids)
            ->select();
        $operation_list = [];
        foreach ($datas as $data) {
            $operation_list[$data['id']] = $data;
        }
        $question_answer_list = RecitingQuestionAnswer::whereIn('id',$ids)->where('user_id',$userid)->select();
        foreach ($list as &$va){
            
            $va['operation']    = [];
            $va['endtime']      = time();
            $va['isend']        = 0;
            $va['finish_ratio'] = 0;
            $va['cuonum']       = 0;

            if (isset($operation_list[$va['mission_id']])) {
                $va['operation'] = $operation_list[$va['mission_id']];
                $va['endtime'] = $operation_list[$va['mission_id']]['deadlinetime'];
                $va['isend'] = 0;
                if (strtotime($operation_list[$va['mission_id']]['deadlinetime'])<=time()) {
                    $va['isend'] = 1;
                }
                //$va['finish_ratio'] = 0;
                if ($operation_list[$va['mission_id']]['uidarr']) {
                    $c = json_decode($operation_list[$va['mission_id']]['uidarr'],true);
                    $user_count = is_array($c)?count($c):0;
                } else {
                    $user_count = User::where('find_in_set(:class_id,class_id)', ['class_id' => $operation_list[$va['mission_id']]['cid']])->count();
                }
                if($user_count > 0){
                    $va['finish_ratio'] = round($operation_list[$va['mission_id']]['frequency']/$user_count*100);
                }else{
                    $va['finish_ratio'] = 0;
                }
                $va['cuonum'] = 0;
                foreach ($question_answer_list as $question_answer_list){
                    if ($question_answer_list['mission_id'] == $va['mission_id'] && $question_answer_list['mission_id'] != 'true_answer_options_id'){
                        $va['cuonum']++;
                    }
                }
            }
        }
        unset($question_answer_list);
        unset($va);
        $this->success('请求成功',$list);
    }

}